package com.zj.core.adapter

import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v7.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.zj.core.R
import com.zj.core.bean.VideoMesBean
import com.zj.core.interfaces.VideoItemIml
import com.zj.core.ui.VideoItemUi
import org.jetbrains.anko.AnkoContext
import org.jetbrains.anko.find

class VideoItemAdapter(context: Context, mDetailList: List<VideoMesBean.DatEntity.RowsEntity>?) :RecyclerView.Adapter<VideoItemAdapter.VideoItemAdapterHolder>() {
    private var mContext: Context? = context
    private var mList:List<VideoMesBean.DatEntity.RowsEntity>? = mDetailList
    private var mListener: VideoItemIml?=null

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoItemAdapterHolder {
        return VideoItemAdapterHolder(VideoItemUi().createView(AnkoContext.create(parent.context,parent)))
    }


    override fun onBindViewHolder(holder: VideoItemAdapterHolder, p1: Int) {
        with(holder){
            recordImg.text= "${p1+1}、"
            recordValueTv.text = mList?.get(p1)?.title
            recordConstraint.setOnClickListener{
                mListener?.setItemClickListener(p1, mList?.get(p1)!!)
            }
        }
    }

    override fun getItemCount(): Int {
        return mList?.size!!
    }

    fun notifiDataChange(dataList:List<VideoMesBean.DatEntity.RowsEntity>?){
        this.mList=dataList
        notifyDataSetChanged()
    }

    inner class VideoItemAdapterHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val recordConstraint: LinearLayout =itemView.find(R.id.record_item_parent_view)
        val recordImg: TextView =itemView.find(R.id.record_item_form_img)
        val recordValueTv: TextView =itemView.find(R.id.record_item_form_value)
    }

    fun setItemListener(iml: VideoItemIml){
        this.mListener=iml
    }
}